package com.javaee.web;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.javaee.ejb.entity.Employee;

/**
 * Servlet implementation class ServletBase
 */
public abstract class HttpServletBase extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected Employee currentEmployee;
	protected HttpSession session;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HttpServletBase() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doRequest(request, response);
	}

	private void setAttributes(HttpServletRequest request) {
		session = request.getSession(true);
		currentEmployee = (Employee) session.getAttribute("currentEmployee");
	}

	protected void loadJSP(String url, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (url == null) {
			return;
		}

		RequestDispatcher rd = request.getRequestDispatcher(url);
		rd.forward(request, response);
	}

	private void doRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		setAttributes(request);

		String requestURI = request.getRequestURI();

		boolean isAdminPage = requestURI.equals(SiteParameters
				.getFullServletPath("administration"))
				|| requestURI.equals(SiteParameters
						.getFullServletPath("addEmployee"))
				|| requestURI.equals(SiteParameters
						.getFullServletPath("addProject"));
		boolean isAuthenticated = (currentEmployee != null);
		boolean isIndexPage = requestURI.equals(SiteParameters
				.getFullServletPath("index"))
				|| requestURI.equals(SiteParameters.siteRoot)
				|| requestURI.equals(SiteParameters
						.getFullServletPath("authentication"));
		boolean isLogoutPage = requestURI.equals(SiteParameters
				.getFullServletPath("logout"));

		if (isLogoutPage) {
			// On continue normalement
			processRequest(request, response);
		} else {
			if (!isIndexPage && !isAuthenticated) {
				// Redirection vers la page d'authentification si on n'est pas
				// sur la page d'index et qu'on n'est pas authentifié
				session.setAttribute("accessPageFailed",
						new String(request.getRequestURI()));
				response.sendRedirect(SiteParameters
						.getFullServletPath("index"));
			} else if (isAuthenticated && currentEmployee.getIsAdmin()
					&& !isAdminPage) {
				response.sendRedirect(SiteParameters
						.getFullServletPath("administration"));
			} else if (isAuthenticated && !currentEmployee.getIsAdmin()
					&& (isAdminPage || isIndexPage)) {
				response.sendRedirect(SiteParameters
						.getFullServletPath("dashboard"));
			} else {
				// Sinon on continue normalement
				processRequest(request, response);
			}
		}
	}

	protected abstract void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException;

}
